package com.cz.run.config.plus;

import com.baomidou.mybatisplus.entity.GlobalConfiguration;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

/**
 * @Description: TODO()
 * @auther:CAIYJ
 * @Date:2018/5/14 9:45
 */
@Configuration
public class MybatisPlusConfig {
    //  mybatisPlus全局配置
    @Bean(name = "basisGlobalConfig")
    public GlobalConfiguration globalConfig(
            @Value("${mybatisPlus.globalConfig.idType}") Integer idType, //主键类型  0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
            @Value("${mybatisPlus.globalConfig.dbColumnUnderline}") Boolean dbColumnUnderline, //驼峰下划线转换
            @Value("${mybatisPlus.globalConfig.isCapitalMode}") Boolean isCapitalMode //数据库大写下划线转换
    ) {
        GlobalConfiguration globalConfig = new GlobalConfiguration();
        // 主键类型
        if (!StringUtils.isEmpty(idType)) {
            globalConfig.setIdType(idType);
        }
        // 驼峰下划线转换
        if (!StringUtils.isEmpty(dbColumnUnderline)) {
            globalConfig.setDbColumnUnderline(dbColumnUnderline);
        }
        // 数据库大写下划线转换
        if (!StringUtils.isEmpty(isCapitalMode)) {
            globalConfig.setCapitalMode(isCapitalMode);
        }
        return globalConfig;
    }

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor().setDialectType("mysql");
    }
}
